package com.wpzf.zxhc.utils;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectRequest;

import java.io.File;

public class OBSUploadUnits {
    private final static String ak = "4WZVUWLRATVVHJM5XDWJ";
    private final static String sk = "p1cRDzHjp39eapYvadx3JUQT6cqqlNXyCsDs0s5Y";
    private final static String endPoint = "https://obs.cn-north-1.myhuaweicloud.com";
    public static String OBSUpload(String OBSAddress,String localAddress) {
        // 创建ObsClient实例
        // 使用永久AK/SK初始化客户端
        ObsClient obsClient = new ObsClient(ak, sk,endPoint);
        // 使用临时AK/SK和SecurityToken初始化客户端
        // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);
        try {
            // 文件上传
            // localfile 为待上传的本地文件路径，需要指定到具体的文件名
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName("obs-top");
            request.setObjectKey(OBSAddress);
            request.setFile(new File(localAddress));
            obsClient.putObject(request);
            System.out.println("putObject successfulluy");
            return "success";
        } catch (ObsException e) {
            System.out.println("putObject failed");
            // 请求失败,打印http状态码
            System.out.println("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            System.out.println("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            System.out.println("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            e.printStackTrace();
            return e.toString();
        } catch (Exception e) {
            System.out.println("putObject failed");
            // 其他异常信息打印
            e.printStackTrace();
            return e.toString();
        }
    }
}

